package PerlBot::Plugin::LogWriter;
use strict;
use warnings;
use base 'Class::Component::Plugin';
use DateTime;

sub log_write : Hook('log.write') {
    my ($self, $c, $args) = @_;

    $self->config->{dir} or die "can't find log directory";

    unless (-d $self->config->{dir}) {
	mkdir $self->config->{dir} or die "can't create a directory";
	$c->log( info => "created a directory $self->config->{dir} ");
    }
			 
    my $dt = DateTime->now( time_zone => 'Asia/Seoul' );
    my $channel = $args->{where};
    $channel =~ s/#//;
    my $path = $self->config->{dir}.$channel;
    unless (-d $path) {
	mkdir $path or die "can't create a directory";
	$c->log( info => "created a directory $path" );
    }
    open my $fh, ">>", $path."/".$dt->ymd.".log";
    print $fh $dt->hms." | $args->{who} | $args->{what}\n";
    close $fh;
}
1;
